package com.lanou.beautifulwardrobe.Brand;

import android.app.AlertDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;

import com.lanou.beautifulwardrobe.R;
import com.lanou.beautifulwardrobe.entify.EditRecommendProduct;

import java.util.List;

/**
 * Created by dllo on 15/10/31.
 */
public class EditDialogFragment extends DialogFragment {
    private EditViewPager viewPagerAdapter;
    private ViewPager viewPager;
    private List<EditRecommendProduct> datas;
    private int index;


    public EditDialogFragment(List<EditRecommendProduct> datas, int index) {
        this.datas = datas;
        this.index = index;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        //getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        View view = inflater.inflate(R.layout.dialog_viewpager, null);
        return view;
        // viewPagerAdapter = new EditViewPager(getChildFragmentManager());
        // return new ViewPager(getActivity());
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        // viewPager = (ViewPager) view;
        viewPager = (ViewPager) view.findViewById(R.id.dialogViewpager);
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        View view = LayoutInflater.from(getContext()).inflate(R.layout.dialog_viewpager, null);
        viewPager = (ViewPager) view.findViewById(R.id.dialogViewpager);
        AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
        AlertDialog mAlertDialog = builder.create();
        mAlertDialog.show();
        mAlertDialog.getWindow().setContentView(view);
        WindowManager m = getActivity().getWindowManager();
        Display d = m.getDefaultDisplay();  //为获取屏幕宽、高
        android.view.WindowManager.LayoutParams p = mAlertDialog.getWindow().getAttributes();  //获取对话框当前的参数值
        p.height = (int) (d.getHeight());   //高度设置为屏幕的0.3
        p.width = (int) (d.getWidth());    //宽度设置为全屏
        mAlertDialog.getWindow().setAttributes(p);
        // mAlertDialog.getWindow().setLayout(360,640);
        return mAlertDialog;//super.onCreateDialog(savedInstanceState);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Log.i("lanou", datas.size() + "***");
        viewPagerAdapter = new EditViewPager(getChildFragmentManager());
        if (datas != null && datas.size() > 0) {
            viewPagerAdapter.addData(datas);
        }
        viewPager.setAdapter(viewPagerAdapter);
        viewPager.setCurrentItem(index);
    }

}
